﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace MVCBugTracker.Areas.UserManagement.Models
{
    public class UserMembershipService : IMembershipService
    {

        private readonly MembershipProvider Provider;

        public UserMembershipService() : this(null) { }

        public UserMembershipService(MembershipProvider provider)
        {
            Provider = provider ?? Membership.Provider;
        }

        public string ApplicationName
        {
            get
            {
                return Provider.ApplicationName;
            }
            set
            {
                Provider.ApplicationName = value;
            }
        }

        public bool EnablePasswordReset
        {
            get { return Provider.EnablePasswordReset; }
        }

        public bool EnablePasswordRetrieval
        {
            get { return Provider.EnablePasswordRetrieval; }
        }

        public int MaxInvalidPasswordAttempts
        {
            get { return Provider.MaxInvalidPasswordAttempts; }
        }

        public int MinRequiredNonAlphanumericCharacters
        {
            get { return Provider.MinRequiredNonAlphanumericCharacters; }
        }

        public int MinRequiredPasswordLength
        {
            get { return Provider.MinRequiredPasswordLength; }
        }

        public int PasswordAttemptWindow
        {
            get { return Provider.PasswordAttemptWindow; }
        }

        public string PasswordStrengthRegularExpression
        {
            get { return Provider.PasswordStrengthRegularExpression; }
        }

        public bool RequiresQuestionAndAnswer
        {
            get { return Provider.RequiresQuestionAndAnswer; }
        }

        public MembershipCreateStatus CreateUser(string userName, string password)
        {
            return (this.CreateUser(userName, password, null, null, null, true, null));
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            return (this.CreateUser(userName, password, email, null, null, true, null));
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved)
        {
            return (this.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, isApproved, null));
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");

            MembershipCreateStatus status;
            Provider.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true, providerUserKey, out status);
            return status;
        }

        public bool DeleteUser(string userName)
        {
            return (this.DeleteUser(userName, false));
        }

        public bool DeleteUser(string userName, bool deleteAllRelatedData)
        {
            return Provider.DeleteUser(userName, deleteAllRelatedData);
        }

        public MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return Provider.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
        }

        public MembershipUserCollection FindUsersByName(string userNameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return Provider.FindUsersByName(userNameToMatch, pageIndex, pageSize, out totalRecords);
        }

        public MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            return Provider.GetAllUsers(pageIndex, pageSize, out totalRecords);
        }

        public int GetNumberOfUsersOnline()
        {
            return Provider.GetNumberOfUsersOnline();
        }

        public MembershipUser GetUser(object providerUserKey)
        {
            return this.GetUser(providerUserKey, false);
        }

        public MembershipUser GetUser(string userName)
        {
            return this.GetUser(userName, false);
        }

        public MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            return Provider.GetUser(providerUserKey, userIsOnline);
        }

        public MembershipUser GetUser(string userName, bool userIsOnline)
        {
            return Provider.GetUser(userName, userIsOnline);
        }

        public string GetUserNameByEmail(string emailToMatch)
        {
            return Provider.GetUserNameByEmail(emailToMatch);
        }

        public void UpdateUser(MembershipUser user)
        {
            Provider.UpdateUser(user);
        }

        public bool ValidateUser(string userName, string password)
        {
            return Provider.ValidateUser(userName, password);
        }
    }
}